package m202311.d14;

/**
 * @author haocongshun
 * @date 2023/11/14 19:27:50
 */

/**
 * 14. 最长公共前缀
 */
public class Q14 {
    public static void main(String[] args) {
        System.out.println(new Q14().longestCommonPrefix(new String[]{
                "ab", "a"
        }));
    }

    public String longestCommonPrefix(String[] strs) {
        char[] array = strs[0].toCharArray();
        int end = array.length;
        for (int i = 1; i < strs.length; i++) {
            for (int i1 = 0; i1 < Math.min(end, strs[i].length()); i1++) {
                if (array[i1] != strs[i].charAt(i1)) {
                    end = i1;
                    break;
                }
            }
            end = Math.min(end, strs[i].length());
            if (end == 0) {
                return "";
            }
        }
        return new String(array, 0, end);
    }
}
